﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace _012_MSMQServer
{
    public class MessageArray
    {

        public int[] Array { get; set; }
        public String Queue { get; set; }


    }

    public class MessageIndex
    {

        public int Index { get; set; }
        public String Queue { get; set; }

        public int Command { get; set; }

    }
    class Program
    {
        static string queue_name = @".\private$\input";
        static void Main(string[] args)
        {
            if (!MessageQueue.Exists(queue_name)) 
                MessageQueue.Create(queue_name); ;
            MessageQueue queue_server = new MessageQueue(queue_name);
            queue_server.Formatter = new XmlMessageFormatter(new Type[]{typeof(MessageArray),typeof(MessageIndex)});
            Server server = new Server();

            while (true)
            {
                Message msg = queue_server.Receive();

                if (msg.Body is MessageArray)
                {
                    Console.WriteLine("# Array received");
                    MessageArray input = (MessageArray)msg.Body;
                    MessageIndex output = new MessageIndex();

                    output.Index =  server.AddRequest(input.Array);

                    if (!MessageQueue.Exists(input.Queue)) MessageQueue.Create(input.Queue); ;
                    MessageQueue queue_client = new MessageQueue(input.Queue);
                    queue_client.Formatter = new XmlMessageFormatter();

                    queue_client.Send(new Message(output));
                    Console.WriteLine("# Response sent {0}",output.Index);
                }
                else
                {
                    if (msg.Body is MessageIndex)
                    {
                        MessageIndex input = (MessageIndex)msg.Body;
                        if (!MessageQueue.Exists(input.Queue)) MessageQueue.Create(input.Queue); ;
                        MessageQueue queue_client = new MessageQueue(input.Queue);
                        queue_client.Formatter = new XmlMessageFormatter();

                        Console.WriteLine("# Command recived, index={0}",input.Index);

                        switch (input.Command)
                        {
                            case 3:
                                {
                                    MessageArray output = new MessageArray();
                                    output.Array = server.GetRequest(input.Index);
                                    queue_client.Send(new Message(output));
                                    Console.WriteLine("# Request sent");
                                }
                                break;
                            case 4:
                                server.ProcessRequest(input.Index);
                                Console.WriteLine("# Array processed");
                                break;
                            case 5:
                                {
                                    MessageArray output = new MessageArray();
                                    output.Array = server.GetResponse(input.Index);
                                    queue_client.Send(new Message(output));
                                    Console.WriteLine("# Response sent");
                                }
                                break;
                        }
                    }
                }
            }
        }
    }
}
